package com.stylefeng.guns.core.util;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

public class AliMsgUtils {
	//产品名称:云通信短信API产品,开发者无需替换
    private static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    private static final String domain = "dysmsapi.aliyuncs.com";
    // TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
    private static final String accessKeyId = "24905784";
    private static final String accessKeySecret = "5d5a5b8daa62f023bfb3916cf93e62a8";
    private static final String signName = "途途行";
    private static final String templateCode = "SMS_10641267"; 
    //阿里大于请求地址
    private static final String dyHttpUrl = "http://gw.api.taobao.com/router/rest"; 
    public static SendSmsResponse sendSms(String phone,String code) throws ClientException {

		//阿里短信平台
        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest request = new SendSmsRequest();
        //必填:待发送手机号
        request.setPhoneNumbers(phone);
        //必填:短信签名-可在短信控制台中找到
        request.setSignName(signName);
        //必填:短信模板-可在短信控制台中找到
        request.setTemplateCode(templateCode);
        //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
        HashMap hm =new HashMap();
        hm.put("code", code);
        hm.put("product",signName);
        String jsonString = JSON.toJSONString(hm);
        request.setTemplateParam(jsonString);

        //选填-上行短信扩展码(无特殊需求用户请忽略此字段)
        //request.setSmsUpExtendCode("90997");

        //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
        //request.setOutId("yourOutId");

        //hint 此处可能会抛出异常，注意catch
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        return sendSmsResponse;    	
    }    
    //阿里大于短信平台
    @SuppressWarnings("unchecked")
	public static AlibabaAliqinFcSmsNumSendResponse sendDySms(String phone,String code) throws ApiException{
    	/*TaobaoClient client = new DefaultTaobaoClient(dyHttpUrl, accessKeyId, accessKeySecret);
    	AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
    	req.setExtend("123456");
    	req.setSmsType("normal");
    	req.setSmsFreeSignName(signName);*/
    	HashMap hm =new HashMap();
        hm.put("code", code);
        hm.put("product",signName);
        /* 
        String jsonString = JSON.toJSONString(hm);
    	req.setSmsParamString(jsonString);
    	req.setRecNum(phone);
    	req.setSmsTemplateCode(templateCode);
    	AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
    	*/
    	//System.out.println(rsp.getBody());
    	//System.out.println(rsp.getParams()); 
    	return sendSmsNotice(phone,templateCode,hm);
    }
    public static AlibabaAliqinFcSmsNumSendResponse sendSmsNotice(String phone,String templateId,Map<String,String>hm) throws ApiException {

    	TaobaoClient client = new DefaultTaobaoClient(dyHttpUrl, accessKeyId, accessKeySecret);
    	AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
    	req.setExtend("");
    	req.setSmsType("normal");
    	req.setSmsFreeSignName(signName);    	
        String jsonString = JSON.toJSONString(hm);
       // System.out.println("aa:"+jsonString);
    	req.setSmsParamString(jsonString);
    	req.setRecNum(phone);
    	req.setSmsTemplateCode(templateId);
    	AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
    	//System.out.println(rsp.getBody());
    	//System.out.println(rsp.getParams()); 
    	return rsp;
    }
    /*
    public static AlibabaAliqinFcSmsNumSendResponse verificationCodeSend(String phone, String from, String to, String time, String tel) throws ApiException{
        TaobaoClient client = new DefaultTaobaoClient(dyHttpUrl, accessKeyId, accessKeySecret);
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        JSONObject json = new JSONObject();
        json.put("from", from);
        json.put("to", to);
        json.put("time", time);
        json.put("tel", tel);
        System.out.println("bb:"+JSON.toJSONString(json));
        req.setExtend("");
        req.setSmsType("normal");
        req.setSmsFreeSignName("途途行");    //短信签名，改为可配置
        req.setSmsParamString(JSON.toJSONString(json));
        req.setRecNum(phone);
        req.setSmsTemplateCode("SMS_144853288");  //短信模板code，改为可配置
        AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);       
        return rsp;
    }*/
    public static void main(String[] args) throws UnsupportedEncodingException {
    	try {
            AlibabaAliqinFcSmsNumSendResponse rsp = AliMsgUtils.sendDySms("15391355103","0569");
            String body = rsp.getBody().toString();
            System.out.println("短信验证码返回bodyhan："+body);
           // AliMsgUtils.sendDySms("15391355103","100001");
    	 /*	
    	    HashMap hm =new HashMap();
            hm.put("from", "东川");  
            hm.put("to", "昆明");
            hm.put("time", "2018-09-21 10:53");  
            hm.put("tel", "0871-68289777");  
    		System.out.println(sendSmsNotice("15391355103","SMS_144853288",hm).getBody());*/
    		
    		/*HashMap hm =new HashMap();
            hm.put("driver", "东川测试");  
            hm.put("drivertel", "15391355103");
            hm.put("carid", "云A555cJ");  
            hm.put("time", "2018-09-21 10:53");  
            hm.put("tel", "0871-68289777");  
    		System.out.println(sendSmsNotice("15391355103","SMS_144853289",hm).getBody());*/
    		//sendDySms("15391355103","99110569");
    		// AliMsgUtils.verificationCodeSend("15391355103", "东川", "昆明", "2018-09-21 10:53", "0871-68289777");
    	} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
